#include "LinkMqttClient.h"
#include "string.h"
#include "RealTimeData.h"
using namespace controller;

std::string pub1, pub2, pub3;

std::map<std::string, std::string> init_params(unsigned long _id, char *_key, char *_url)
{
    std::map<std::string, std::string> m;
    char buf[200];
    memset(buf, 0, sizeof(buf));
    sprintf(buf, "%010lX", _id);
    std::string deviceid(buf);
    std::string key(_key);
    std::string url(_url);
    std::string sub1(deviceid + "_sub/back");
    std::string sub2(deviceid + "_sub/com");
    std::string sub3("$iot/public/com/" + deviceid);

    pub1 = std::string("$iot/real/" + deviceid);
    pub2 = std::string("$iot/control/" + deviceid);
    pub3 = std::string("$iot/public/comback/" + deviceid);

    m["deviceid"] = deviceid;
    m["url"] = url;
    m["key"] = key;
    m["sub1"] = sub1;
    m["sub2"] = sub2;
    m["sub3"] = sub3;

    return m;
}

void test_LinkMqttClient(void)
{
    LinkMqttClient l;
    l.open(init_params(0x01608B0240, (char *)"e567b535", (char *)"tcp://58.42.237.171:1883"));

    std::cout << "开始发送数据" << std::endl;
    RealTimeDataItemJson rj;
    sql::time t = sql::time::now();
    rj.tm = t.get();
    rj.ch = 2;
    rj.stcd = "01608B0240";

    RealTimeData rt;
    rt.to_inv_solar(rj.values);
    // rt.to_sms(2,rj.values);
    // rt.to_ib(rj.values);
    // rt.to_yu(rj.values);
    // rt.to_duanluqi(rj.values);
    string str = Json::class2jsonstring(rj);
    // std::cout << "publish:" << str << std::endl;
    //  l.write((char *)str.c_str(), str.size(), pub1.c_str());

    while (1)
    {
        if (l.is_open())
        {
            l.write((char *)str.c_str(), (int)str.size(), pub1.c_str());
        }
        sleep(5);
        // l.close();
    }
}